package com.ternnetwork.batch.processor.demo;


import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;

import com.ternnetwork.batch.dao.demo.CreditBillDao;
import com.ternnetwork.batch.model.demo.CreditBill;

public class CreditBillProcessor implements ItemProcessor<CreditBill,CreditBill>{
	
    private static final Logger logger= LoggerFactory.getLogger(CreditBillProcessor.class);
    
    private String batchNo;
    
    private Map<String,CreditBill> map;
    
    @Resource
    private CreditBillDao creditBillDao;
    private static List<CreditBill> list;
    
	public CreditBill process(CreditBill t) throws Exception {
		// merge(t);
		 /*
		 if(list==null){
			 list=creditBillDao.findAll("from CreditBill", null);
		 }
		 
		 logger.info("{}",t.toString());
	     return merge(list, t);*/
		t.setBatchNo(batchNo);
		System.out.println(t.toString());
		return t;
	}

	private CreditBill merge(CreditBill t) {
		if(map==null){
			 map=new ConcurrentHashMap<String,CreditBill>();
		 }
		 
		 if(map.get(t.getAccountId())!=null){
			 return null;
		 }
		 map.put(t.getAccountId(), t);
		 return t;
	}
	
	private CreditBill merge(List<CreditBill> list,CreditBill t){
		for(CreditBill c:list){
			if(c.getAccountId().equals(t.getAccountId())){
				c.setAddress("888888888");
				return c;
			} 
		}
		return t;
	}

	public String getBatchNo() {
		return batchNo;
	}

	public void setBatchNo(String batchNo) {
		this.batchNo = batchNo;
	}

}
